/*
 * @lc app=leetcode.cn id=102 lang=javascript
 *
 * [102] 二叉树的层序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
 var levelOrder = function(root) {
  //二叉树的层序遍历
  let res=[],queue=[];
   queue.push(root)
   if (root === null) {
     return res
   }
   while (queue.length !== 0) {
     let length = queue.length
     let curLevel = []
     for (let i = 0; i < length; i++){
      let cur = queue.shift()
      curLevel.push(cur.val)
      cur.left && (queue.push(cur.left))
      cur.right && (queue.push(cur.right))
       
     }
     res.push(curLevel)

   }
  return res;
};
// @lc code=end

